package material.command.undo;

/**
 * 高速指令
 */
public class CeilingFanHighCommand implements Command {
    CeilingFan ceilingFan;
    int preSpeed;//增加局部状态以便追踪吊扇之前的状态

    public CeilingFanHighCommand(CeilingFan ceilingFan) {
        this.ceilingFan = ceilingFan;
    }

    @Override
    public void execute() {
        /*在execute()中，
        在我们改变吊扇的速度之前,需先将它之前的状态记录起来,以便需要撤销时使用。*/
        preSpeed = ceilingFan.getSpeed();
        ceilingFan.setHigh();
    }

    /**
     * 将吊扇的速度设置回之前的值达到撤销的目的
     */
    @Override
    public void undo() {
        if (preSpeed == CeilingFan.HIGH) {
            ceilingFan.setHigh();
        } else if (preSpeed == CeilingFan.MEDIUM) {
            ceilingFan.setMedium();
        } else if (preSpeed == CeilingFan.LOW) {
            ceilingFan.setLow();
        } else if (preSpeed == CeilingFan.OFF) {
            ceilingFan.setOff();
        }

    }
}
